from panda3d.core import *
from direct.showbase import Audio3DManager
from panda3d.physics import *
from direct.interval.ParticleInterval import *
from direct.particles.Particles import Particles
from direct.particles.ParticleEffect import ParticleEffect
import random, sys, os, math,time

class Weapon():
    def __init__(self,name,type,damage,model,offset,heading,rate,reload,ammo,clip,mclip,canscope,isscoped,spray,recoil,auto,fsound,scale = Vec3(0.01,0.01,0.01)):
        random.seed(time.clock())
        #preload
        loader.loadModel(model)
        self.name = name
        self.type = type
        self.damage = damage
        self.model = model
        self.baseoffset = offset
        self.offset = self.baseoffset
        self.heading = heading
        self.rate = rate
        self.reload = reload
        self.ammo = ammo
        self.clip = clip
        self.maxclip = mclip
        self.canscope = canscope
        self.isscoped = isscoped
        self.spray = spray
        self.recoil = recoil
        self.automatic = auto
        self.canfire = True
        self.lastfired = 0
        self.firesound = base.loader.loadSfx(fsound)
        self.bulletcount = 0
        self.scale = scale
        self.p = ParticleEffect()
        self.p.loadConfig("particles/bullet.ptf")
    def Fire(self):
        time = globalClock.getFrameTime()
        if time - self.lastfired >= self.rate:
            if self.automatic == False:
                if self.canfire:
                    if self.clip > 0:
                        self.lastfired = time
                        #self.firesound.play()
                        self.BulletMath()
                        self.canfire = False
                        self.clip -= 1
                    else:
                        self.Reload()
            else:
                if self.clip > 0:
                    self.BulletMath()
                    #self.firesound.play()
                    self.lastfired = time
                    self.clip -= 1
                else:
                    self.Reload()
    def Reload(self):
        self.clip += self.maxclip
        print "reloading"
    def Drop(self):
        self.model = ""

    def BulletMath(self):
        randomSpray = random.randint(-self.spray,+self.spray)
        randomRecoil = random.randint(0,self.recoil)
        player = base.Player
        base.CamController.pitch += randomRecoil/2
        base.CamController.heading -= randomSpray/2
        self.offset = self.baseoffset - Vec3(0,0.002,-0.001)
        pFrom = Point3(player.dummy.getPos()+Vec3(player.dummy.getMat().getRow3(1))*1 )
        dir =  Vec3(player.dummy.getMat().getRow3(1))
        result = base.Physics.doRaycast(pFrom, dir, 9999)
        if result.hasHit():
            try:
                if "Ladder" in result.getNode().getName():
                    pass
                elif "Map-mesh" in result.getNode().getName():
                    self.BulletGraphic(result)
                else:
                    if base.client.ClientList[result.getNode().getName()].team != base.Player1.team:
                        base.client.writeTask("HIT",result.getNode().getName())
                    else:
                        print "TEAM KILL _DEV_"
            except:
                print sys.exc_info()[1]
                print "Collision error _DEV_"
    def BulletGraphic(self,result):
        b = ParticleEffect()
        b.loadConfig("particles/bullet.ptf")
        b.setPos(result.getHitPos())
        b.start(render,render)
        cm = CardMaker('Plane')
        card = render.attachNewNode(cm.generate())
        card.setScale(0.3)
        card.lookAt(Point3(-result.getHitNormal()))
        card.setPos(result.getHitPos()+result.getHitNormal()*0.01*random.randint(1,3))
        tex = loader.loadTexture("textures/bullet_concrete.png")
        card.setTransparency(TransparencyAttrib.MAlpha)
        card.setTexture(TextureStage.getDefault(),tex)
        base.Player.graphiclist.append(b)
        base.Player.lastbullethit.append(globalClock.getFrameTime())


